<script setup>
import {
  deleteStockShortageApplication,
  queryStockShortageApplicationList,
} from '@/service/stock-shortage-application.js'
import { reactive, ref, watch } from 'vue'
import StockShortageApplicationDialog from '@/pages/StockShortageApplication/components/StockShortageApplicationDialog.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
const applicationList = ref([])

const searchForm = reactive({
  productName: null,
  brandName: null,
  status: -1,
  currentPage: 1,
  pageSize: 10,
})
const totalRecords = ref(0)

function onTapResetButton() {
  Object.assign(searchForm, {
    productName: null,
    brandName: null,
    status: -1,
    currentPage: 1,
    pageSize: 10,
  })
}
async function loadData() {
  const res = await queryStockShortageApplicationList(
    searchForm.productName,
    searchForm.brandName,
    searchForm.status,
    searchForm.currentPage,
    searchForm.pageSize
  )
  console.log(res.data)
  totalRecords.value = res.data.total
  applicationList.value = res.data.records
}

watch(
  searchForm,
  () => {
    loadData()
  },
  { immediate: true }
)
const stockShortageApplicationDialogRef = ref()
function onTapCheckButton(row) {
  stockShortageApplicationDialogRef.value.applicationForm.status = row.status
  stockShortageApplicationDialogRef.value.applicationForm.productName = row.productName
  stockShortageApplicationDialogRef.value.applicationForm.brandName = row.brandName
  stockShortageApplicationDialogRef.value.applicationForm.productSpecificationName = row.productSpecificationName
  stockShortageApplicationDialogRef.value.applicationForm.id = row.id
  stockShortageApplicationDialogRef.value.applicationForm.productImages = row.productImages
  stockShortageApplicationDialogRef.value.showModal = true
}
function onTapDeleteButton(row) {
  ElMessageBox.confirm('确定删除该条缺货申请吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'danger',
  })
    .then(async () => {
      await deleteStockShortageApplication(row.id)
      await loadData()
      ElMessage({
        type: 'success',
        message: '成功删除！',
      })
    })
    .catch(() => {})
}
</script>

<template>
  <div class="stock-shortage-application">
    <el-form :inline="true" :model="searchForm">
      <el-form-item label="商品名称:">
        <el-input v-model="searchForm.productName" placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <el-form-item label="品牌名称:">
        <el-input v-model="searchForm.brandName" placeholder="请输入品牌名称"></el-input>
      </el-form-item>
      <el-form-item label="处理状态:">
        <el-select v-model="searchForm.status" style="width: 240px">
          <el-option :value="-1" label="全部" />
          <el-option :value="0" label="未处理" />
          <el-option :value="1" label="已处理" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="onTapResetButton">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="applicationList"
      :header-cell-style="() => ({ background: '#eef1f6', color: '#606266' })"
      border
      style="width: 100%"
    >
      <el-table-column align="center" label="ID">
        <template #default="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="商品名称">
        <template #default="scope">
          <span v-if="scope.row.productName">{{ scope.row.productName }}</span>
          <span v-else style="color: #0fc6c2">未提交</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="品牌">
        <template #default="scope">
          <span v-if="scope.row.brandName">{{ scope.row.brandName }}</span>
          <span v-else style="color: #0fc6c2">未提交</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="规格型号">
        <template #default="scope">
          <span v-if="scope.row.productSpecificationName">{{ scope.row.productSpecificationName }}</span>
          <span v-else style="color: #0fc6c2">未提交</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="登记时间">
        <template #default="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 1" type="success">已处理</el-tag>
          <el-tag v-else type="warning">未处理</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="175">
        <template #default="scope">
          <el-button type="primary" @click="onTapCheckButton(scope.row)">查看</el-button>
          <el-button type="danger" @click="onTapDeleteButton(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="searchForm.currentPage"
      v-model:page-size="searchForm.pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :total="totalRecords"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 10px"
    />
  </div>
  <StockShortageApplicationDialog
    ref="stockShortageApplicationDialogRef"
    @update="loadData"
  ></StockShortageApplicationDialog>
</template>

<style lang="scss" scoped>
.stock-shortage-application {
  background-color: #fff;
  padding: 20px;
}
</style>
